<template>
  <div class="chart">
    <el-row :gutter="20">
      <el-col :span="12">
        <h1>能耗同比分析（千瓦时）</h1>
        <div id="graded" class="echart"></div
      ></el-col>
      <el-col :span="12">
        <h1>金额同比分析（元）</h1>
        <div id="money" class="echart"></div
      ></el-col>
    </el-row>
  </div>
</template>

<script>
import echarts from "@/utils/lib/echarts";
import { BarChart, LineChart } from "echarts/charts";
echarts.use([BarChart, LineChart]);
export default {
  name: "analysisChart",
  props: {
    // 折线图数据
    data: {
      type: Object,
      default: () => {
        return {
          gradedList: [],
          moneyList: [],
        };
      },
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.BarChart(this.data);
  },
  beforeDestroy() {
    this.chartDes();
  },
  methods: {
    chartDes() {
      if (!this.chart) {
        return;
      }
      this.chart.dispose();
      this.chart = null;
    },
    BarChart(data) {
      let option1 = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            crossStyle: {
              color: "#999",
            },
          },
        },
        legend: {
          data: ["本期能耗", "同期能耗", "同比", "累计同比"],
        },
        xAxis: [
          {
            type: "category",
            data: [
              "1月",
              "2月",
              "3月",
              "4月",
              "5月",
              "6月",
              "7月",
              "8月",
              "9月",
            ],
            axisPointer: {
              type: "shadow",
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            name: "num",
            min: 0,
            max: 100,
            interval: 50,
            axisLabel: {
              formatter: "{value} kwh",
            },
          },
          {
            type: "value",
            name: "percentage",
            min: 0,
            max: 25,
            interval: 5,
            axisLabel: {
              formatter: "{value} %",
            },
          },
        ],
        series: [
          {
            name: "本期能耗",
            type: "bar",
            color: "#5B9BD5",
            tooltip: {
              valueFormatter: function (value) {
                return value + " kwh";
              },
            },
            data: [12.0, 14.9, 17.0, 23.2, 25.6, 76.7, 80.72, 92.34, 66.71],
          },
          {
            name: "同期能耗",
            type: "bar",
            color: "#ED7D31",
            tooltip: {
              valueFormatter: function (value) {
                return value + " kwh";
              },
            },
            data: [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 68.94, 74.22, 52.14],
          },
          {
            name: "同比",
            type: "line",
            yAxisIndex: 1,
            color: "yellow",
            tooltip: {
              valueFormatter: function (value) {
                return value + " %";
              },
            },
            data: [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 11.4, 14.4, 9.8, 5.7],
          },
          {
            name: "累计同比",
            type: "line",
            yAxisIndex: 1,
            color: "red",
            tooltip: {
              valueFormatter: function (value) {
                return value + " %";
              },
            },
            data: [4.0, 4.2, 5.3, 6.5, 7.7, 12.2, 18.4, 22.4, 14.3, 8.7],
          },
        ],
      };
      let option2 = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            crossStyle: {
              color: "#999",
            },
          },
        },
        legend: {
          data: ["本期金额", "同期金额", "同比", "累计同比"],
        },
        xAxis: [
          {
            type: "category",
            data: [
              "1月",
              "2月",
              "3月",
              "4月",
              "5月",
              "6月",
              "7月",
              "8月",
              "9月",
            ],
            axisPointer: {
              type: "shadow",
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            name: "money",
            min: 0,
            max: 200,
            interval: 100,
            axisLabel: {
              formatter: "{value} 元",
            },
          },
          {
            type: "value",
            name: "percentage",
            min: 0,
            max: 25,
            interval: 5,
            axisLabel: {
              formatter: "{value} %",
            },
          },
        ],
        series: [
          {
            name: "本期金额",
            type: "bar",
            color: "#7F6000",
            tooltip: {
              valueFormatter: function (value) {
                return value + " 元";
              },
            },
            data: [44.24, 44.9, 47.0, 44.22, 72.6, 143.7, 144.8, 167.9, 121.2],
          },
          {
            name: "同期金额",
            type: "bar",
            color: "#0070C0",
            tooltip: {
              valueFormatter: function (value) {
                return value + " 元";
              },
            },
            data: [46.61, 47.91, 42.02, 50.4, 64.7, 133.7, 132.8, 145.9, 102.2],
          },
          {
            name: "同比",
            type: "line",
            yAxisIndex: 1,
            color: "",
            tooltip: {
              valueFormatter: function (value) {
                return value + " %";
              },
            },
            data: [3.0, 2.2, 6.3, 4.5, 6.3, 7.2, 13.4, 16.7, 18.9, 12.4, 10.1],
          },
          {
            name: "累计同比",
            type: "line",
            yAxisIndex: 1,
            color: "",
            tooltip: {
              valueFormatter: function (value) {
                return value + " %";
              },
            },
            data: [4.0, 4.2, 6.3, 7.5, 7.7, 12.2, 16.4, 18.4, 20.9, 14.4, 15.1],
          },
        ],
      };
      this.initChart("graded", option1);
      this.initChart("money", option2);
    },
    initChart(id, options) {
      this.chart = echarts.init(document.getElementById(id));
      this.chart.setOption(options);
      window.addEventListener("resize", () => {
        if (this.chart) {
          this.chart.resize();
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
h1 {
  text-align: center;
  font-weight: bold;
}
.chart {
  .echart {
    width: 100%;
    height: 420px;
  }
}
</style>
